import React, { ReactElement, useEffect } from 'react';
import { BackHandler } from 'react-native';
import { NavigationScreenProps } from 'react-navigation';
import KeepAwake from 'react-native-keep-awake';

import Provider from './reducer';
import List from './components/List';
import {
  back,
  initClassRoom,
  addListener,
  removeListener,
  clearAllTimer,
  clearPPTPlayer,
  clearSeekTimeList
} from './actions';
import { ClassRoomType, ProblemRoundStatus } from './constant';
import {
  endAsyncClassRoomInfo,
  startAsyncClassRoomInfo
} from '../../store/classRoom';
import { setAudioAvailable } from '../../util/RTC/rtcEngine';
import httpServer from '../../util/native/httpServer';

export default ({ navigation }: NavigationScreenProps): ReactElement => {
  const lessonId = navigation.getParam('lessonId', 0);
  const startRoundId = navigation.getParam('startRoundId', 0);
  const roundStatus = navigation.getParam(
    'roundStatus',
    ProblemRoundStatus.Unknown
  );
  const channel = navigation.getParam('channel', '');
  const type = navigation.getParam('type', ClassRoomType.Live);
  const lessonResourceId = navigation.getParam('lessonResourceId', 0);
  const welcomeData = navigation.getParam('welcomeData', {
    startImageURL: '',
    startVideoURL: ''
  });
  const intervalBeforeLessonStart = navigation.getParam(
    'intervalBeforeLessonStart',
    0
  );
  const lessonName = navigation.getParam('lessonName', '');
  const lessonSource = navigation.getParam('lessonSource', '');
  const needSign = navigation.getParam('needSign', false);
  const assistantInfo = navigation.getParam('assistantInfo', {});
  const startTime = navigation.getParam('startTime', '');
  const handleBackButton = (): boolean => {
    back();
    return true;
  };

  useEffect((): (() => void) => {
    initClassRoom({
      lessonId,
      startRoundId,
      channel,
      type,
      lessonResourceId,
      welcomeData,
      intervalBeforeLessonStart,
      lessonName,
      lessonSource,
      roundStatus,
      needSign,
      assistantInfo,
      lessonStartTime: startTime
    });

    addListener(type);
    BackHandler.addEventListener('hardwareBackPress', handleBackButton);
    KeepAwake.activate();

    if (type === ClassRoomType.Vod) {
      startAsyncClassRoomInfo();
    }

    return (): void => {
      httpServer.stop();
      clearPPTPlayer();
      removeListener();
      clearAllTimer();
      BackHandler.removeEventListener('hardwareBackPress', handleBackButton);
      KeepAwake.deactivate();

      setAudioAvailable(false, 'leaveClassRoom');
      clearSeekTimeList();

      if (type === ClassRoomType.Vod) {
        endAsyncClassRoomInfo();
      }
    };
  }, [
    channel,
    intervalBeforeLessonStart,
    lessonId,
    lessonResourceId,
    startRoundId,
    type,
    welcomeData,
    lessonName,
    lessonSource,
    roundStatus,
    needSign,
    assistantInfo,
    startTime
  ]);

  return (
    <Provider>
      <List />
    </Provider>
  );
};
